package com.example.demo.service;

import com.example.demo.Mapper.BookMapper;
import com.example.demo.enums.BookStatusEnum;
import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.List;

@Service
public class BookService {


    @Autowired
    private BookMapper bookMapper;

    //插入图书
    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    //查询图书信息
    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest){
        //1, 总记录数
        Integer count = bookMapper.count();

//        List<BookInfo> bookInfos = bookDao.mockData();
        //        //2. 当前页记录数

        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest.getOffset(),pageRequest.getPageSize());
        for(BookInfo bookInfo : bookInfos){
            if(bookInfo.getStatus() == 0){
                bookInfo.setStatusCN("删除");
            }else if(bookInfo.getStatus() == 1){
                bookInfo.setStatusCN("可借阅");
            }else{
                bookInfo.setStatusCN("不可借阅");
            }
//            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        System.out.println();
        return new PageResponse<BookInfo>(count,bookInfos,pageRequest);


    }

    //通过id查询图书
    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    //更新图书
    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
         bookMapper.batchDelete(ids);
    }
}
